package com.ca.ridersclubapplication.request;

import android.app.Activity;

import com.ca.ridersclubapplication.data.DataHandler;
import com.ca.ridersclubapplication.data.entity.Token;
import com.ca.ridersclubapplication.request.dto.ChatMessageDto;
import com.ca.ridersclubapplication.request.dto.MsgListDto;
import com.ca.ridersclubapplication.request.dto.Result;

import java.util.List;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Query;

public class MessageRequester {


    public static String URL = "message/";

    public static final MessageAPI api;

    static {
        api = new Retrofit.Builder()
                .baseUrl(Config.BASE_URL + URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(MessageAPI.class);
    }

    public static interface MessageAPI {

        @POST("send")
        @FormUrlEncoded
        Call<Result<String>> sendMessage(
                @Field("targetId") Long targetId,
                @Field("content") String content,
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @POST("setRead")
        @FormUrlEncoded
        Call<Result<String>> setRead(
                @Field("targetId") Long targetId,
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @POST("setReadAll")
        Call<Result<String>> setReadAll(
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @GET("listMsgForTarget")
        Call<Result<List<ChatMessageDto>>> listMsgForTarget(
                @Query("targetId") Long targetId,
                @Query("startId") Long startId,
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @GET("listNewMsg")
        Call<Result<List<ChatMessageDto>>> listNewMsg(
                @Query("targetId") Long targetId,
                @Query("endId") Long endId,
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @GET("listMsgList")
        Call<Result<List<MsgListDto>>> listMsgList(
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);

        @POST("deleteMsgListByTarget")
        @FormUrlEncoded
        Call<Result<String>> deleteMsgListByTarget(
                @Field("targetId") Long targetId,
                @Header("Authorization") String token,
                @Header("arg0") String hid,
                @Header("arg1") String email);
    }

    public static void sendMessage(Activity activity, Long targetId, String content, AfterExecutor<String> executor) {
        Token token = DataHandler.getToken(activity);
        api.sendMessage(targetId, content, token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void setRead(Activity activity, Long targetId, AfterExecutor<String> executor) {
        Token token = DataHandler.getToken(activity);
        api.setRead(targetId, token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void setReadAll(Activity activity, AfterExecutor<String> executor) {
        Token token = DataHandler.getToken(activity);
        api.setReadAll(token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void listMsgForTarget(Activity activity, Long targetId, Long startId, AfterExecutor<List<ChatMessageDto>> executor) {
        Token token = DataHandler.getToken(activity);
        api.listMsgForTarget(targetId, startId, token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void listNewMsg(Activity activity, Long targetId, Long endId, AfterExecutor<List<ChatMessageDto>> executor) {
        Token token = DataHandler.getToken(activity);
        api.listNewMsg(targetId, endId, token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void listMsgList(Activity activity, AfterExecutor<List<MsgListDto>> executor) {
        Token token = DataHandler.getToken(activity);
        api.listMsgList(token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }

    public static void deleteMsgListByTarget(Activity activity, Long targetId, AfterExecutor<String> executor) {
        Token token = DataHandler.getToken(activity);
        api.deleteMsgListByTarget(targetId, token.getToken(), String.valueOf(token.getId()), token.getEmail())
                .enqueue(new APICallback<>(activity, executor));
    }
}
